package com.zjzywl.smoke.controller;

import com.zjzywl.smoke.authenticate.AuthUser;
import com.zjzywl.smoke.config.AppConfig;
import com.zjzywl.smoke.config.ResultConfig;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileNotFoundException;

public class BaseController extends ResultConfig{
	protected Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	public AppConfig appConfig;

	@Autowired
	public RestTemplate restTemplate;

	public AuthUser getLoginUser() {
		return (AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	}

	public String getUserId() {
		try {
			AuthUser user = getLoginUser();
			return user.getPeople().getUserId().toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("获取登录用户信息失败，错误信息为" + e.getMessage());
			return "";
		}

	}

	String getUploadDir() throws Exception {
		String path = System.getProperty("user.home") + "/iot/" + appConfig.getUploadDir() + "/";
		File file =  new File(path);
		if (file.isDirectory() && !file.exists()) {
			if(!file.mkdirs()) {
				throw new FileNotFoundException();
			}
		}
		return path;
	}

}